<?php
ob_start();
session_start();
	require_once('db.php');
	include('functions.php');
	
	if (array_key_exists('check_submit', $_POST))
	{
		if ( $_POST['name'] != '' && $_POST['password'] != '' )
		{          
            $name = $_POST['name'];
            $pass = $_POST['password'];
                                                                   
			//$query = mysql_query ( 'SELECT id, name, status FROM user WHERE name = "' .  ( $_POST['name'] ) . '" AND pass = "' .  ( md5($_POST['password'] ) ) . '"' );
            $query = "select * from user where name = '$name' and pass = '$pass'";
            
			
			if ( mysql_num_rows(mysql_query($query)) == 1 )
			{
				$row = mysql_fetch_assoc ( mysql_query($query) );
                print_r($row['id']);
				if ( $row['status'] == 1 )
				{
					
					$_SESSION['user_id'] = $row['id'];
					$_SESSION['logged_in'] = TRUE;
                    //session_commit();
					header ("Location: ../");
				}

				elseif ( $row['status'] == 0 ) {
					$error = 'Bạn đã bị khóa nick!';
				}
			}
			else {		
				$error = 'Login failed!';		
			}
		}
		else {
			$error = 'Hãy điền đầy đủ cả username và password!';
		}
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Media-Music Login Page</title>
	<link href="css/login_styles.css" rel="stylesheet" type="text/css" />

</head>                                        

<body>

	<div id="log">
<?php if(isset($error)){ echo '			<p class="error">' . $error . '</p>' . "\n";}?>
	</div>
	<div id="container">

		<form class="form" action="login.php" method="post">

			<input type="hidden" name="check_submit" value="1"/>   		
			<div style="margin-top:15px; margin-bottom:19px">
				<img src="images/username.gif" alt="name" border="0" />
				<input class="input" type="text" name="name" id="name" size="32" maxlength="40" value="" />
			</div>
			<div style="margin-bottom:9px">
				<img src="images/password.gif" alt="password" border="0" />
				<input class="input" type="password" name="password" id="password" size="32" maxlength="32" />

			</div>
			<input type="image" name="Login" value="Login"  class="submit-btn" src="images/btn.gif" alt="submit" title="submit" />
			<br class="clear" />
			<a href="register.php">Register</a>
			
		</form>
		
	</div>
	
</body>

</html>